<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      size="small"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="上级菜单" :disabled="unitParentState">
        <el-popover
          ref="unitListPopover"
          placement="bottom-start"
          trigger="click"
          :disabled="unitParentState"
        >
          <el-tree
            :data="unitList"
            :props="unitListTreeProps"
            node-key="id"
            ref="unitListTree"
            @current-change="unitListTreeCurrentChangeHandle"
            :default-expand-all="false"
            :highlight-current="true"
            :expand-on-click-node="false"
          >
          </el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:unitListPopover
          :disabled="unitParentState"
          :readonly="true"
          placeholder="点击选择上级菜单"
          class="menu-list__input"
        ></el-input>
      </el-form-item>

      <el-form-item label="单位名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="单位名称"></el-input>
      </el-form-item>

      <el-form-item label="单位编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="单位编码"></el-input>
      </el-form-item>

      <el-form-item label="附加值1">
        <el-input v-model="dataForm.field1" placeholder="附加值1"></el-input>
      </el-form-item>

      <el-form-item label="排序号" prop="orderNum">
        <el-input-number
          v-model="dataForm.orderNum"
          controls-position="right"
          :min="0"
          label="排序号"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="单位状态" size="mini">
        <el-radio-group v-model="dataForm.dataState">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input
          type="textarea"
          v-model="dataForm.remark"
          placeholder="备注"
        ></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>


<script>
import { pageList, saveOrUpdate } from "@/api/unit";
import { treeDataTranslate } from "@/utils";

export default {
  data() {
    return {
      visible: false,
      unitParentState: false,
      dataForm: {
        id: "",
        code: "",
        parentName: "",
        parentId: 0,
        name: "",
        remark: "",
        field1: "",
        orderNum: 0,
        dataState: 0,
      },
      dataRule: {
        name: [
          { required: true, message: "单位名称不能为空", trigger: "blur" },
        ],
        code: [
          { required: true, message: "单位编码不能为空", trigger: "blur" },
        ],
      },
      dataListLoading: "",
      unitList: [],
      unitListTreeProps: {
        label: "name",
        children: "children",
      },
    };
  },
  components: {},
  activated() {
    // this.getTreeList();
  },
  methods: {
    init(data) {
      this.unitParentState = false;
      this.dataForm.id = 0;
      if (data) {
        this.dataForm.id = data.id || 0;
        this.unitParentState = true;
        // 添加子节点
        if (data.addChildren) {
          data.parentId = data.id;
          this.dataForm.id = 0;
        }
      }
      pageList()
        .then((res) => {
          this.dataListLoading = true;
          if (res && res.code === 0) {
            this.unitList = treeDataTranslate(res.data, "id");
          }
          this.dataListLoading = false;
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (!this.dataForm.id) {
            const parentId = 0;
            if (data) {
              parentId = data.parentId;
            } else {
              parentId = this.dictList[0].id;
            }
            this.dataForm.parentId = parentId;
            // 新增
            this.unitListTreeSetCurrentNode();
          } else {
            // 修改
            this.dataForm.id = data.id;
            this.dataForm.name = data.name;
            this.dataForm.parentId = data.parentId;
            this.dataForm.url = data.url;
            this.dataForm.orderNum = data.orderNum;
            this.dataForm.remark = data.remark;
            this.dataForm.dataState = data.dataState;
            this.dataForm.field1 = data.field1;
            this.dataForm.code = data.code;
            this.unitListTreeSetCurrentNode();
          }
        });
    },
    // 菜单树选中
    unitListTreeCurrentChangeHandle(data, node) {
      this.dataForm.parentId = data.id;
      this.dataForm.parentName = data.name;
    },
    // 菜单树设置当前选中节点
    unitListTreeSetCurrentNode() {
      this.$refs.unitListTree.setCurrentKey(this.dataForm.parentId);
      this.dataForm.parentName = (this.$refs.unitListTree.getCurrentNode() ||
        {})["name"];
    },
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          saveOrUpdate({
            id: this.dataForm.id || "",
            parentId: this.dataForm.parentId || 0,
            name: this.dataForm.name,
            code: this.dataForm.code,
            remark: this.dataForm.remark,
            orderNum: this.dataForm.orderNum,
            field1: this.dataForm.field1,
            dataState: this.dataForm.dataState,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.unitParentState = false;
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
